package com.topfinance.cnaps2.service.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.topfinance.cnaps2.ebo.SysParamCtrlEbo;
import com.topfinance.cnaps2.service.IParamCtrlManager;
import com.topfinance.frame.dao.HibernateEntityDao;
import com.topfinance.frame.exception.AppException;
import com.topfinance.frame.web.QueryReq;

/**
 * 该类自动实现了dao的一般方法，如add, remove, update
 * 在service中出现的大部分情况下是在向后台提交数据前的处理(如校验)及获取后台数据后的处理等
 * @author
 *
 */

public class ParamCtrlManagerImpl extends HibernateEntityDao<SysParamCtrlEbo> implements IParamCtrlManager {

	private static final Log log = LogFactory.getLog(ParamCtrlManagerImpl.class);

	public void addParam(SysParamCtrlEbo o) throws AppException{
    	save(o);
    }
	
	public void editParam(SysParamCtrlEbo o) throws AppException{
    	saveOrUpdate(o);
    }
	
	//获得传输协议	
	public SysParamCtrlEbo getParam(Integer ctrlid) throws AppException{
		return (SysParamCtrlEbo)get(ctrlid);
	}

	public void deleteParam(SysParamCtrlEbo o) throws AppException{
		removeById(o.getClass(),o.getCtrlId());
    }

	//获得传输协议列表
	public List getAllParamList(String paramCatgs,String code) throws AppException{
		String catgids[]= null;
		if(paramCatgs!=null && !"".equals(paramCatgs)){
			catgids = paramCatgs.split(",");
		}
		String where = "";
		String select = "select new com.topfinance.cnaps2.dto.SysParamCtrlDto";
		select += "(e.type,e.code,e.name) from SysParamCtrlEbo e";
		String orderBy = " ctrlId asc";
		if(catgids != null && catgids.length>0){
			where += " e.catg in (" ;
			for(int i=0; i<catgids.length ;i++){
				if(i==(catgids.length-1)){
					where += "'"+ catgids[i]+"' ";
				}else{
					where += "'"+ catgids[i]+"',";
				}				
			}
			where += " )" ;
		}
		
		if(code != null && code.length() > 0){
			where += " and e.type = '" + code + "'"; 
		}
		
		where = " where 1=1 and " + where;
		
		QueryReq queryReq = new QueryReq();
		log.debug("sql:" + select);
		queryReq.setSelect(select);
		if(!"".equals(where))
		   queryReq.setWhere(where);
		queryReq.setOrderBy(orderBy);
		try {
			return find(select + where);
		} catch (Exception e) {
			return null;
		}		
	}
}
